package  com.suredy.fw.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.suredy.core.ctrl.BaseFlowCtrl;
import com.suredy.fw.model.FWInfo;
import com.suredy.fw.service.FWInfoSrv;

@Controller
@RequestMapping({"/test/appfw"})
public class FWInfoFormCtrl extends BaseFlowCtrl {
	
	@Autowired
	private FWInfoSrv fwInfoSrv;

	@RequestMapping({"/save"})
	@ResponseBody
	public boolean save(@ModelAttribute("form") FWInfo fwInfo) {
		
		fwInfo = (FWInfo) this.fwInfoSrv.update(fwInfo);
		this.updateTodo(fwInfo, fwInfo.getTitle(),"/test/appfw/fwform/"+fwInfo.getId()+".do");
		return true;
	}

	@RequestMapping({"/fwform/{id}"})
	public ModelAndView formInfo(@PathVariable String id) {
		ModelAndView view = new ModelAndView("/test/appfw/fwform");
		FWInfo fwInfo = (FWInfo) this.fwInfoSrv.get(FWInfo.class, id);
		view.addObject("fwInfo", fwInfo);
		return view;
	}

	@RequestMapping({"/list"})
	public ModelAndView getList() {
		ModelAndView view = new ModelAndView("/test/appfw/view_list");
		List<FWInfo> l = this.fwInfoSrv.readByEntity(new FWInfo(), 1, 20);
		int count = 0;
		if (l != null && !l.isEmpty()) {
			count = this.fwInfoSrv.getCountByEntity(new FWInfo());
			count = count < 0 ? 0 : count;
		}
		view.addObject("data", l);
		view.addObject("pageSize", 20);
		view.addObject("pageIndex", 1);
		view.addObject("count", count);
		return view;
	}

	@RequestMapping({"/new"})
	@ResponseBody
	public String newFile() {
		FWInfo fwInfo = new FWInfo();
		fwInfo.setTitle("新建发文表单");
		fwInfo.setFileTypeCode("fw");
		this.createFlow(fwInfo, "fw", "F=test/APP=SuredyOA/O=suredy", "/test/appfw/fwform/{id}");
		return this.createTodoUri("/test/appfw/fwform/{id}", fwInfo.getId());
	}
	
	@RequestMapping({"/newtemp"})
	@ResponseBody
	public ModelAndView newFileTemp() {
		
		FWInfo fwInfo = new FWInfo();
		fwInfo.setTitle("新建发文表单");

		this.createFlow(fwInfo, "123", "F=test/APP=SuredyOA/O=suredy", "/test/appfw/fwform/{id}");
		ModelAndView view = new ModelAndView( new RedirectView("fwform/"+fwInfo.getId()));
		return view;
	}
}
